package com.cjw.service.impl.personal;

import com.cjw.enums.share.CommentType;
import com.cjw.enums.share.SharingType;
import com.cjw.mapper.database.UserMapper;
import com.cjw.mapper.livesquare.LiveSharingMapper;
import com.cjw.mapper.livesquare.LiveUserMapper;
import com.cjw.mapper.soulread.ReadMapper;
import com.cjw.mapper.soulread.ReadUserMapper;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.dto.livesquare.LiveSharingDTO;
import com.cjw.pojo.query.livesquare.QueryLiveUserDTO;
import com.cjw.pojo.dto.soulread.ReadCollectionDTO;
import com.cjw.pojo.entity.database.User;
import com.cjw.pojo.entity.livesquare.LiveUser;
import com.cjw.pojo.entity.soulread.Read;
import com.cjw.pojo.entity.soulread.ReadUser;
import com.cjw.pojo.query.soulread.ReadCollectionQuery;
import com.cjw.service.livesquare.CommentService;
import com.cjw.service.person.ShareCollectionService;
import com.cjw.service.soulread.HeatService;
import com.cjw.util.BaseContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @create_time : 2021.12.8
 * @author : cjw
 * @descripe: 生活分享收藏服务实现
 */
@Service("shareCollectionService")
public class ShareCollectionServiceImpl implements ShareCollectionService {
    @Autowired
    private CommentService commentService;

    @Autowired
    private LiveSharingMapper liveSharingMapper;

    @Autowired
    private LiveUserMapper liveUserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HeatService heatService;

    @Autowired
    private ReadMapper readMapper;

    @Autowired
    private ReadUserMapper readUserMapper;

    @Override
    public List<LiveSharingDTO> findAllLiveSharing(Integer page, Integer size) {
        List<LiveSharingDTO> liveSharingDTOS = new ArrayList<>();
        List<LiveUser> allSelfCollection = getCollectionLiveSharing(page,size, SharingType.COLLECTION.getCode());
        List<LiveUser> allSelfLove = getCollectionLiveSharing(page,size,SharingType.LOVE.getCode());
        for (LiveUser liveUser: allSelfCollection) {
            LiveSharingDTO liveSharingDTO = liveSharingMapper.queryLiveDTO(liveUser.getLiveId(), page * size, size);
            liveSharingDTO.setCollection(true);
            for (LiveUser liveUser1 : allSelfLove) {
                if (liveUser1.getLiveId() == liveUser.getLiveId()) {
                    liveSharingDTO.setLove(true);
                }
            }
            liveSharingDTOS.add(liveSharingDTO);
        }
        return liveSharingDTOS;
    }

    @Override
    public PageDTO<List<ReadCollectionDTO>> findAllCollectionRead(Integer page, Integer size) {
        PageDTO<List<ReadCollectionDTO>> listPageDTO = new PageDTO<>();
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        ReadUser readUser = new ReadUser();
        readUser.setUserId(user.getId());
        readUser.setType(SharingType.COLLECTION.getCode());
        listPageDTO.setTotal(readUserMapper.selectCount(readUser));
        if (listPageDTO.getTotal() == 0) {
            return listPageDTO;
        }

        ReadCollectionQuery readCollectionQuery = new ReadCollectionQuery();
        readCollectionQuery.setUserId(user.getId());
        readCollectionQuery.setPage(page * size);
        readCollectionQuery.setSize(size);
        listPageDTO.setDtos(toReadCollectionDTO(readCollectionQuery));
        return listPageDTO;
    }

    /**
     * 转换为ReadCollectionDTO
     * @param readCollectionQuery
     * @return
     */
    private List<ReadCollectionDTO> toReadCollectionDTO(ReadCollectionQuery readCollectionQuery) {
        ArrayList<ReadCollectionDTO> readCollectionDTOS = new ArrayList<>();
        ReadUser readUser = new ReadUser();

        for (ReadUser readUser1 : readUserMapper.findAllPage(readCollectionQuery)) {
            ReadCollectionDTO readCollectionDTO = new ReadCollectionDTO();
            readCollectionDTO.setId(readUser1.getReadId());
            Read read = new Read();
            read.setId(readUser1.getReadId());
            Read read1 = readMapper.selectOne(read);
            readCollectionDTO.setAddTime(read1.getAddTime());
            readCollectionDTO.setTopic(read1.getTopic());
            readCollectionDTO.setHeat(heatService.getHeat(read1.getId()).intValue());
            readUser.setReadId(readUser1.getReadId());
            readUser.setType(SharingType.LOVE.getCode());
            readCollectionDTO.setLoveNum(readUserMapper.selectCount(readUser));
            readCollectionDTO.setCommentNum(commentService.getCommentNum(readUser1.getReadId(), CommentType.READ.getCode()));
            User user = new User();
            user.setId(readUser1.getUserId());
            readCollectionDTO.setAuthName(userMapper.selectOne(user).getUserName());
            readCollectionDTOS.add(readCollectionDTO);
        }
        return readCollectionDTOS;
    }

    /**
     * 分页获取点赞/收藏列表
     * @param page
     * @param size
     * @param type
     * @return
     */
    private List<LiveUser> getCollectionLiveSharing(Integer page, Integer size, Integer type) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        QueryLiveUserDTO queryLiveUserDTO = new QueryLiveUserDTO();
        queryLiveUserDTO.setBegin(page*size);
        queryLiveUserDTO.setSize(size);
        queryLiveUserDTO.setPower(0);
        queryLiveUserDTO.setUserId(user.getId());
        queryLiveUserDTO.setType(type);
        return liveUserMapper.getAllSelf(queryLiveUserDTO);
    }
}
